package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import Dailog.PayDialog;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.GoodsPay;
import bean.OrderDetail;
import event.OrderEvent;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;

/**
 * author : ChenHuiMing
 * date : 2018/9/25
 * email : 907399255@qq.com
 * description : 订单支付
 */
public class OrderPayFragment extends BaseNetFragment {

    public static final String GET_ORDER_URL = "/Shop/getOrder";
    public static final String PAYMENT_URL = "/Shop/payment";

    private TextView mTvBack;
    private ImageView mIvLogo;
    private TextView mTvName;
    private TextView mTvTeacher;
    private TextView mTvOrderTime;
    private TextView mTvOrderNo;
    private LinearLayout mLlAliPay;
    private LinearLayout mLlWeixinPay;
    private CheckBox mCbAliPay;
    private CheckBox mCbWeixinPay;
    private TextView mTvPrice;
    private TextView mTvPay;
    private LinearLayout mLlCourseMoney;
    private TextView mTvCourseMoney;
    private TextView mTvPayStatus;

    private OrderDetail mOrderDetail;
    private PayDialog mPayDialog;

    public static Fragment getInstance() {
        return new OrderPayFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (TextUtils.equals(action, GET_ORDER_URL)) {
            mOrderDetail = (OrderDetail) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), OrderDetail.class);
            if (baseBean.code == 200) {
                ImageLoaderUtils.loadImage(getActivity(), mOrderDetail.schedule_image, mIvLogo);
                mTvName.setText(mOrderDetail.schedule_name);
                mTvTeacher.setText("主讲：" + mOrderDetail.teacher_name);
                mTvOrderTime.setText("订单生成时间:" + mOrderDetail.create_time);
                mTvOrderNo.setText("订单号:" + mOrderDetail.order_no);
                mTvCourseMoney.setText("￥" + mOrderDetail.amount);
                mTvPrice.setText("￥" + mOrderDetail.price);
            } else {
                Toast.makeText(getActivity(), baseBean.getMsg(), Toast.LENGTH_LONG).show();
            }
        } else if (TextUtils.equals(action, PAYMENT_URL)) {
            final GoodsPay goodsPay = (GoodsPay) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), GoodsPay.class);
            if (baseBean.getCode() == 200) {
                if (!TextUtils.isEmpty(goodsPay.qr_code_img)) {
                    if (mPayDialog != null && mPayDialog.isShowing()) {
                        mPayDialog.dismiss();
                    }
                    mPayDialog = new PayDialog(getActivity(), goodsPay.qr_code_img, goodsPay.order_id);
                    mPayDialog.setOnPayListener(new PayDialog.OnPayListener() {
                        @Override
                        public void onPay(boolean isSucceed) {
                            if (isSucceed) {
                                toast("支付成功");
                                OrderEvent orderEvent = new OrderEvent();
                                orderEvent.order_id = goodsPay.order_id;
                                orderEvent.pagerIndex = 2;
                                EventBus.getDefault().post(orderEvent);
                            }
                        }
                    });
                    mPayDialog.show();
                }
            } else {
                Toast.makeText(getActivity(), baseBean.getMsg(), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_pay;
    }

    @Override
    protected void findViews() {
        mTvBack=getView(R.id.tv_back);
        mIvLogo = getView(R.id.iv_logo);
        mTvName = getView(R.id.tv_name);
        mTvTeacher = getView(R.id.tv_teacher);
        mTvOrderTime = getView(R.id.tv_order_time);
        mTvOrderNo = getView(R.id.tv_order_no);
        mLlAliPay = getView(R.id.ll_ali_pay);
        mLlWeixinPay = getView(R.id.ll_wechat_pay);
        mCbAliPay = getView(R.id.cb_ali_pay);
        mCbWeixinPay = getView(R.id.cb_wechat_pay);
        mTvPrice = getView(R.id.tv_price);
        mTvPay = getView(R.id.tv_pay);
        mLlCourseMoney = getView(R.id.ll_course_money);
        mTvCourseMoney = getView(R.id.tv_course_money);
        mTvPayStatus = getView(R.id.tv_pay_status);
    }

    @Override
    protected void formatViews() {
        mLlCourseMoney.setVisibility(View.VISIBLE);
        mTvPayStatus.setVisibility(View.GONE);
    }

    @Override
    protected void formatData() {
        EventBus.getDefault().register(this);
        mTvBack.setOnClickListener(this);
        mTvPay.setOnClickListener(this);
        mLlAliPay.setOnClickListener(this);
        mLlWeixinPay.setOnClickListener(this);
        mCbAliPay.setOnClickListener(this);
        mCbWeixinPay.setOnClickListener(this);
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View view) {
        if (view==mTvBack){
            OrderEvent orderEvent = new OrderEvent();
            orderEvent.pagerIndex = 0;
            EventBus.getDefault().post(orderEvent);
        } else if (view == mLlAliPay || view == mCbAliPay) {
            mCbAliPay.setChecked(true);
            mCbWeixinPay.setChecked(false);
        } else if (view == mLlWeixinPay || view == mCbWeixinPay) {
            mCbAliPay.setChecked(false);
            mCbWeixinPay.setChecked(true);
        } else if (view == mTvPay) {
            Map<String, Object> map = new HashMap<>();
            map.put("order_id", mOrderDetail.order_id);
            map.put("order_type", 2);
            map.put("pay_type", mCbAliPay.isChecked() ? 2 : 1);
            post(PAYMENT_URL, map, BaseBean.class, false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OrderEvent event) {
        if (event.pagerIndex == 1) {
            Map<String, Object> map = new HashMap<>();
            map.put("order_id", event.order_id);
            post(GET_ORDER_URL, map, BaseBean.class, false);
        }
    }
}
